<!DOCTYPE html>
<html lang="en" xmlns:th="http:www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>error</title>
    <link rel="stylesheet" href="/js/elementui/index.css">
    <style>
        .lan{
            width: 300px;
            margin-right: 20px;
        }
    </style>
</head>
<body>
    <div id="app">
        <span>车辆名称:</span><el-input v-model="input1" placeholder="请输入车辆名称" class="lan">&ensp;&ensp;</el-input> <span>车牌号:</span>
        <el-input v-model="input2" placeholder="请输入车牌号" class="lan"></el-input>
        <el-button icon="el-icon-search" type="primary" @click="selectOne">查询</el-button><br>
        <br>
        <el-button type="warning" icon="el-icon-circle-plus-outline" @click="dialogFormVisible = true">添加车辆</el-button>
        <el-table
                :data="tableData"
                style="width: 100%">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    label="车辆编号"
            >
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.vehicleId }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    label="车辆名称"
            >
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.vehicleName }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    label="车牌号"
            >
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.numberPlate }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    label="车辆类型"
            >
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.vehicleType }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    label="所属物流公司"
            >
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.logisticsName }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    label="驾驶员"
            >
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.driverName }}</span>
                </template>
            </el-table-column>


            <el-table-column
                    label="车辆载重量(T)"
            >
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.driverCapacity }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    label="尺寸(L/W/H)"
            >
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.driverSize }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="primary"
                            icon="el-icon-edit-outline"
                            @click="handleEdit(scope.$index, scope.row)"></el-button>
                    <el-button
                            size="mini"
                            type="warning"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.$index, scope.row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="curr"
                :page-sizes="[5,10,15,20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>

        <el-dialog title="车辆的操作" :visible.sync="dialogFormVisible" @close="closeMyDialog">
            <el-form :model="vehicle">
                <el-form-item label="车辆名称:" :label-width="formLabelWidth">
                    <el-input v-model="vehicle.vehicleName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="物流公司:" :label-width="formLabelWidth">

                    <el-select v-model="vehicle.logisticsId" placeholder="请选择">
                        <el-option
                                v-for="l in logistics"
                                :label="l.logisticsName"
                                :value="l.logisticsId">
                        </el-option>
                    </el-select>

                    <!--<el-input v-model="vehicle.idCard" autocomplete="off"></el-input>-->
                </el-form-item>
                <el-form-item label="驾驶员:" :label-width="formLabelWidth">

                    <el-select v-model="vehicle.driverId" placeholder="请选择驾驶员">
                        <el-option
                                v-for="d in drivers"
                                :label="d.driverName"
                                :value="d.driverId">
                        </el-option>
                    </el-select>

                </el-form-item>
                <el-form-item label="车牌号:" :label-width="formLabelWidth">
                    <el-input v-model="vehicle.numberPlate" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="车辆类型:" :label-width="formLabelWidth">
                    <el-input v-model="vehicle.vehicleType" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="尺寸(L/W/H):" :label-width="formLabelWidth">
                    <el-input v-model="vehicle.driverSize" autocomplete="off"></el-input>
                </el-form-item>


                <el-form-item label="备注:" :label-width="formLabelWidth">
                    <el-input
                            type="textarea"
                            :rows="2"
                            placeholder="请输入内容"
                            v-model="vehicle.remarks">
                    </el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveRoles">确 定</el-button>
            </div>
        </el-dialog>
    </div>
    <script src="/js/vue/vue.js"></script>
    <script src="/js/axios/axios.min.js"></script>
    <script src="/js/qs/qs.min.js"></script>
    <script src="/js/elementui/index.js"></script>
    <script>
        new Vue({
            el:"#app",
            data(){
                return{
                    //车辆查询的第一个和第二个输入框value值
                    input1:"",
                    input2:"",
                    logistics:"",//查找所有的物流公司
                    drivers:"",//查找所有的驾驶员
                    formLabelWidth: '120px', //表单标签的宽度
                    dialogFormVisible:false,
                    tableData:[],
                    total:0,
                    curr:1,
                    pageSize:5,
                    vehicle:{//车辆的一些属性
                        vehicleId:"",
                        vehicleName:"",
                        numberPlate:"",
                        vehicleType:"",
                        logisticsId:"",
                        driverId:[],
                        driverCapacity:"",
                        driverSize:"",
                        status:"",
                        remarks:"",
                        createId:"",
                        createTime:"",
                        updateTime:"",
                        updateId:"",
                        logisticsName:"",
                        driverName:""
                    },
                }
            },
            //函数定义区域
            methods:{
                handleSizeChange(pageSize) {
                    this.loadPage(this.curr,pageSize)
                },
                handleCurrentChange(curr) {
                    this.loadPage(curr,this.pageSize)
                },
                //车牌号与车辆名称的查询
                selectOne(){
                    this.loadPage(this.curr,this.pageSize)
                },

                handleEdit(index, row) {
                    this.vehicle=row;
                    this.dialogFormVisible=true;
                },
                handleDelete(index, row) {
                    console.log(index, row);
                    var id= row.vehicleId
                    if(confirm("您确定要删除吗")){
                        axios.delete("/vehicle/"+id).then(value => {
                            if(value.data.code==1&&this.total-((this.curr-1)*this.pageSize)==1){
                                this.loadPage(this.curr-1,this.pageSize)
                            }else {
                                this.loadPage(this.curr,this.pageSize)
                            }

                        })
                    }
                },

                //保存用户的方法
                saveRoles(){
                    /*var a="";
                    alert(this.vehicle.driverId)
                    for (let i = 0; i <this.vehicle.driverId.length ; i++) {
                        a+=this.vehicle.driverId[i]+","
                    }
                    alert(a)
                    this.vehicle.driverId=a
                    alert(this.vehicle.driverId)*/
                    if(this.vehicle.vehicleId=='') {
                        axios.post("/vehicle",this.vehicle).then(value => {
                            if (value.data.code == 1) {
                                this.loadPage(Math.ceil((this.total+1)/this.pageSize),this.pageSize)
                            }
                        })
                    }else {
                        axios.put("/vehicle", this.vehicle).then(value => {
                            if (value.data.code == 1) {
                                this.loadPage(this.curr,this.pageSize)
                            }
                        })
                    }
                    this.dialogFormVisible= false;
                },

                //清空复选框
                closeMyDialog(){
                    vehicle={//车辆的一些属性
                        vehicleId:"",
                            vehicleName:"",
                            numberPlate:"",
                            vehicleType:"",
                            logisticsId:"",
                            driverId:[],
                            driverCapacity:"",
                            driverSize:"",
                            status:"",
                            remarks:"",
                            createId:"",
                            createTime:"",
                            updateTime:"",
                            updateId:"",
                            logisticsName:"",
                            driverName:""
                    }
                },

                loadPage(curr,pageSize) {
                    this.curr = curr;
                    this.pageSize = pageSize
                    if(this.input1=='' && this.input2==''){
                        axios.get("/vehicles?current=" + curr + "&pageSize=" + pageSize).then(value => {
                            if (value.data.code == 1) {
                                this.tableData = value.data.data.records;
                                this.curr = value.data.data.current;
                                this.pageSize = value.data.data.size;
                                this.total = value.data.data.total;
                            }
                        })
                    }else {
                        axios.get("/vehicleNameId?current="+this.curr+"&pageSize="+this.pageSize+"&input1="+this.input1+"&input2="+this.input2).then(value => {
                            if (value.data.code == 1) {
                                this.tableData = value.data.data.records;
                                this.curr = value.data.data.current;
                                this.pageSize = value.data.data.size;
                                this.total = value.data.data.total;
                            }
                        })
                    }

                }
            },
            //页面加载成功回调
            mounted() {
                //分页
                this.loadPage(1, 5)

                //查找物流公司
                axios.get("/logisticsAll").then(value => {
                    if(value.data.code==1){
                        this.logistics=value.data.data;
                    }
                });

                //查找所有的驾驶员
                axios.get("/driverAll").then(value => {
                    if(value.data.code==1){
                        this.drivers=value.data.data;
                    }
                })

            }
        })
    </script>
</body>
</html>